package koction.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import koction.model.question.ListVO;
import koction.model.question.QuestionBoardService;
import koction.model.question.QuestionBoardVO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class QuestionBoardController {
	
	@Resource(name="questionBoardServiceImpl")
	private QuestionBoardService service;
	
	@RequestMapping("/getQuestionBoardList.do")	
	public ModelAndView getQuestionBoardList(String pageNo) throws Exception{
		ListVO lvo=service.getQuestionBoardList(pageNo);
		String path="getQuestionBoardList.question";
		return new ModelAndView(path, "lvo", lvo);
	}
	
	@RequestMapping("/ViewWriteQuestionBoard.do")
	public ModelAndView ViewWriteQuestionBoard() throws Exception{
		return new ModelAndView("writeQuestionBoard.question");
	}
	
	@RequestMapping("/writeQuestion.do")
	public ModelAndView writeQuestion(QuestionBoardVO qvo) throws Exception{
		service.writeQuestion(qvo);
		String path="redirect:showContentQuestionBoard.do?no="+qvo.getNo();
		return new ModelAndView(path);
	}
	
	//작성한 게시글의 상세보기
	@RequestMapping("/showContentQuestionBoard.do")
	public ModelAndView showContentQuestionBoard(int no) throws Exception{
		ModelAndView mv=new ModelAndView("showContentQuestionBoard.question","qvo",service.showContentQuestionBoard(no));
		mv.addObject("pageNo", "1");	//현재 페이지 번호(새글 이니 항상 1페이지이다.)
		return mv;
	}
	
	//리스트에서 선택한 게시글의 상세보기
	@RequestMapping("/showContent.do")
	public ModelAndView showContent(int no,int count,String pageNo,HttpServletRequest request) throws Exception{
		request.setAttribute("pageNo", pageNo); //클릭한 게시글의 페이지번호
		return new ModelAndView("showContentQuestionBoard.question","qvo", service.showContent(no,count));
	}
	
	@RequestMapping("/deleteQuestionBoard.do")
	public ModelAndView deleteQuestionBoard(int no, String pageNo) throws Exception{
		service.deleteQuestionBoard(no);
		ListVO lvo=service.getQuestionBoardList(pageNo);
		String path="getQuestionBoardList.question";
		return new ModelAndView(path, "lvo", lvo);
	}
	
	@RequestMapping("/updateQuestionBoardView.do")
	public ModelAndView updateQuestionBoardView(int no, String pageNo) throws Exception{
		ModelAndView mv=new ModelAndView("updateQuestionBoardView.question","qvo",service.showContentQuestionBoard(no));
		mv.addObject("pageNo", pageNo);
		return mv;
	}
	
	@RequestMapping("/updateQuestionBoard.do")
	public ModelAndView updateQuestionBoard(QuestionBoardVO qvo, String pageNo) throws Exception{
		service.updateQuestionBoard(qvo);
		String path="redirect:showContentQuestionBoard.do?no="+qvo.getNo()+"&pageNo="+pageNo;
		return new ModelAndView(path);
	}
	
	@RequestMapping("/replyQuestionBoardView.do")
	public ModelAndView replyQuestionBoardView(int no, String pageNo) throws Exception{
		ModelAndView mv=new ModelAndView("replyQuestionBoardView.question","qvo",service.showContentQuestionBoard(no));
		mv.addObject("pageNo", pageNo);
		return mv;
	}
	
	@RequestMapping("/replyQuestion.do")
	public ModelAndView writeReplyQuestionBoard(QuestionBoardVO qvo, String pageNo) throws Exception{		
		String replyId=service.getReplyId(qvo);
		qvo.setReplyId(replyId);
		service.writeReplyQuestionBoard(qvo);
		String path="redirect:showContentQuestionBoard.do?no="+qvo.getNo()+"&pageNo="+pageNo;
		return new ModelAndView(path);
	}
	
	@RequestMapping("/replyCheck.do")
	@ResponseBody
	public String replyCheck(String ref) throws Exception{
		String result=service.replyCheck(ref);
		return result;
	}
}
